package com.shiguiwu.springmybatis.spring.lifecycle.destroy;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;

/**
 * @description: 12销毁阶段
 *
 * 1. 调用
 * org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#destroyB
 * ean
 * 2. 调用org.springframework.beans.factory.config.ConfigurableBeanFactory#destroySingletons
 * 3. 调用ApplicationContext中的close方法
 *
 * 顺序
 * 1. @PreDestroy标注的所有方法
 * 2. DisposableBean接口中的destroy()
 * 3. 自定义的销毁方法
 * @author: stone
 * @date: Created by 2021/3/28 13:59
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.lifecycle.destroy
 */
public class DestroyTests {

    public static void main(String[] args) {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        factory.addBeanPostProcessor(new MyDestructionAwareBeanPostProcessor());
        //将CommonAnnotationBeanPostProcessor加入
        factory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor()); //@2

        AbstractBeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(Integer.class)
                .addConstructorArgValue(12)
                .getBeanDefinition();
        AbstractBeanDefinition definition1 = BeanDefinitionBuilder.genericBeanDefinition(Integer.class)
                .addConstructorArgValue(12)
                .getBeanDefinition();
        AbstractBeanDefinition definition3 = BeanDefinitionBuilder.genericBeanDefinition(A.class)
                .getBeanDefinition();
        //设置销毁方法
        definition3.setDestroyMethodName("destroy1");

        factory.registerBeanDefinition("shuzi", definition);
        factory.registerBeanDefinition("shuzi1", definition1);
        factory.registerBeanDefinition("A", definition3);
        //触发所有单列初始化
        factory.preInstantiateSingletons();

        //销毁一个
        factory.destroySingleton("shuzi");

        System.out.println("销毁所有的bean");
        factory.destroySingletons();

        //关闭容器
    }
}
